package com.hl.icd.service.impl;

import com.github.pagehelper.PageHelper;
import com.hl.icd.bean.ICD;
import com.hl.icd.bean.ICDHistory;
import com.hl.icd.bean.Version;
import com.hl.icd.dao.ICDDao;
import com.hl.icd.dao.ICDHistoryDao;
import com.hl.icd.dao.VersionDao;
import com.hl.icd.service.ICDService;
import com.hl.icd.utils.DateTimeUtil;
import com.hl.icd.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ICDServiceImpl implements ICDService {

    @Autowired
    private ICDDao icdDao;

    @Autowired
    private ICDHistoryDao icdHistoryDao;

    @Autowired
    private VersionDao versionDao;

    @Override
    public List<ICD> codeQuery(int pageNum, int pageSize,ICD icd) {
        PageHelper.startPage(pageNum,pageSize);
        return icdDao.getICDListByCondition(icd);
    }

    @Override
    public String insertOne(ICD icd) {
        String message="error";
        icd.setId(UUIDUtil.getUUID());
        int res = icdDao.insert(icd);
        if(res == 1){
            message = "success";
        }
        return message;
    }

    @Override
    public String deleteOneById(String id,String editBy) {
        String message="error";
        String editTime = DateTimeUtil.getSysTime();
        ICD icd = icdDao.selectByPrimaryKey(id);
        ICDHistory icdHistory = new ICDHistory(icd,"删除",editBy,editTime);
        int res1 = icdHistoryDao.insert(icdHistory);
        int res2 = icdDao.deleteByPrimaryKey(id);
        if(res1 == 1 && res2 == 1){
            message = "success";
        }
        return message;
    }

    @Override
    public String deleteBatch(List<String> ids,String editBy) {
        String message="success";
        int res1=0;
        int res2=0;
        for (String id : ids) {
            String editTime = DateTimeUtil.getSysTime();
            ICD icd = icdDao.selectByPrimaryKey(id);
            ICDHistory icdHistory = new ICDHistory(icd,"删除",editBy,editTime);
            res1 = icdHistoryDao.insert(icdHistory);
            res2 = icdDao.deleteByPrimaryKey(id);
            if(res1!=1 || res2 !=1){
                message = "error";
                return message;
            }
        }
        return message;
    }

    @Override
    public String updateOne(ICD icd,String editBy) {
        String message="error";
        //提取字符串中的数字
        String typeStr = icd.getType();
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(typeStr);
        icd.setType( m.replaceAll("").trim());

        ICD oldIcd = icdDao.selectByPrimaryKey(icd.getId());
        String editTime = DateTimeUtil.getSysTime();
        ICDHistory icdHistory = new ICDHistory(oldIcd,"更新",editBy,editTime);
        int res1 = icdHistoryDao.replace(icdHistory);

        int res = icdDao.updateByPrimaryKey(icd);
        if(res == 1 && res1 >= 1){
            message="success";
        }
        return message;
    }

    @Override
    public List<Version> getVersionList() {
        return versionDao.selectAll();
    }

    @Override
    public List<ICDHistory> historyQuery(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return icdDao.getHistoryList();
    }

}
